package com.example.juc.big_talk_concurrent.chapter3.article2;

import java.util.concurrent.atomic.AtomicReference;

public class AtomicDemo {

    private static AtomicReference<User> reference = new AtomicReference<>();

    public static void main(String[] args) {
        User user1 = new User("user1", 16);
        reference.set(user1);

        User user2 = new User("user2", 18);
        reference.compareAndSet(user1, user2);

        System.out.println(reference.get().getUserName()); // "user2"
    }

    static class User {
        private String userName;
        private int age;

        public User(String userName, int age) {
            this.userName = userName;
            this.age = age;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
